package com.md.album.ui

import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.living.player.BottomPlayerFragment
import com.md.album.R
import com.md.album.SelfAlbumFragment
import com.md.album.databinding.ActivitySelfcreateAlbumBinding
import com.md.album.db.SelfAlbumManager
import com.md.album.utils.AlbumUtil
import com.md.datastore.MysaveAlbumManager
import com.md.network.api.Album

@Route(path = "/selfcreate/album")
class SelfAlbumActivity:AppCompatActivity() {

    private lateinit var binding :ActivitySelfcreateAlbumBinding
    @JvmField
    @Autowired(name = "SELF_CREATE_TITLE")
    var title:String="默认标题"

    @JvmField
    @Autowired(name = "CREATED_ALBUM_ID")
    var albumId:Int=-1
    @JvmField
    @Autowired(name = "IS_MINE_MINE")
    var isMine:Boolean= true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =  ActivitySelfcreateAlbumBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
        ARouter.getInstance().inject(this)
        var transaction=  supportFragmentManager.beginTransaction()
        var fragment = BottomPlayerFragment()
        var createAlbumFragment = SelfAlbumFragment.newFragment(title,isMine)
        var album =
                if (isMine) AlbumUtil.convertSelfAlbumDBToSlefAlbum(SelfAlbumManager.getIns().getAlbumDBById(albumId.toString()))
                else AlbumUtil.convertMySaveAlbumDBToSlefAlbum(MysaveAlbumManager.getIns().getAlbumDBById(albumId.toString()))
//        var selfAlbumDb = SelfAlbumManager.getIns().getAlbumDBById(albumId.toString())
//        var createdAlbum = if (selfAlbumDb !=null) AlbumUtil.convertSelfAlbumDBToSlefAlbum(selfAlbumDb) else Album(ArrayList(), "", "", "", albumId.toString(),0, title,null) // audios没有从数据库读取，因为SelfAlbumFragment会再读取一次数据库
        createAlbumFragment.setAlbum(album)
        transaction.add(R.id.fragmentContainer,createAlbumFragment)
        transaction.add(R.id.playerContainer,fragment)

        transaction.commit()
    }

}